<?php
//数组中的健值要和id相同
$arr = array(
	1 => array('id' => 1, 'pid' => 0, 'name' => '中国'),
	2 => array('id' => 2, 'pid' => 1, 'name' => '湖北省'),
	3 => array('id' => 3, 'pid' => 1, 'name' => '上海'),
	4 => array('id' => 4, 'pid' => 2, 'name' => '武汉市'),
	5 => array('id' => 5, 'pid' => 2, 'name' => '仙桃市'),
	6 => array('id' => 6, 'pid' => 4, 'name' => '江汉区'),
	7 => array('id' => 7, 'pid' => 4, 'name' => '新洲区'),
	8 => array('id' => 8, 'pid' => 3, 'name' => '闵行区'),
	9 => array('id' => 9, 'pid' => 3, 'name' => '浦东区'),
	10 => array('id' => 10, 'pid' => 7, 'name' => '邾城街'),
);

//查看数组被改变的过程
function get_tree2($arr){
	foreach($arr as $v){
		$arr[$v['pid']]['son'][]=&$arr[$v['id']];
		$i++;
		echo $i;
		echo "<hr>";
		echo "<pre>";
		print_r($arr);
		echo "</pre>";
	}
}

//上面的方法是网络里面找的，下面这个方法是自己写的
function get_tree($arr){
	foreach($arr as $v){
		$arr[$v['pid']]['son'][]=&$arr[$v['id']];
	}
	return $arr[0]['son'];
}

//get_tree2($arr);

echo "<pre>";
print_r(get_tree($arr));
echo "</pre>";



